package org.footballmanager.pandafootball.system.controller;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.form.impl.FootXfSearchForm;
import org.footballmanager.pandafootball.system.service.FootPersonService;
import org.footballmanager.pandafootball.system.service.FootUserService;
import org.footballmanager.pandafootball.system.service.FootXfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@SessionAttributes("footXfSearchForm")
@RequestMapping("/foot/footXf")
public class FootXfController extends BaseController {
    /* 实体名称 */
    private static final String ENTITYNAME = "footXf";
    private static final String LISTPAGE = String.format(Constants.LISTPAGE, Constants.FOOT, ENTITYNAME);
    private static final String VIEWPAGE = String.format(Constants.VIEWPAGE, Constants.FOOT, ENTITYNAME);
    private static final String REDIRECT = String.format(Constants.REDIRECT, Constants.FOOT, ENTITYNAME);

    @Autowired
    private FootXfService footXfService;
    @Autowired
    private FootUserService footUserService;
    @Autowired
    private FootPersonService footPersonService;

    /**
     * 初始化
     *
     * @param map              参数
     * @param footXfSearchForm 条件参数
     */
    @ModelAttribute
    public void init(Model map, @ModelAttribute FootXfSearchForm footXfSearchForm) {
        map.addAttribute("userList", footUserService.getall());
        map.addAttribute("qyList", footPersonService.getall());
        FootXfSearchForm newFootXfSearchForm = new FootXfSearchForm();
        if (footXfSearchForm != null) {
            newFootXfSearchForm = footXfSearchForm;
        }
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newFootXfSearchForm);
    }

    @RequestMapping(value = "/search")
    public String search(Model map, @ModelAttribute FootXfSearchForm footXfSearchForm) {
        footXfService.search(ENTITYNAME, map, footXfSearchForm);
        return LISTPAGE;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(@RequestParam Long id, HttpServletRequest request) {
        footXfService.delete(id, request);
        return REDIRECT;
    }

    @RequestMapping(value = "/deleteall", method = RequestMethod.POST)
    public String deleteall(HttpServletRequest request, @ModelAttribute FootXfSearchForm footXfSearchForm) {
        footXfService.deleteAll(footXfSearchForm, request);
        return REDIRECT;
    }

    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String openViewPage(Model map, @RequestParam Long id, @ModelAttribute FootXfSearchForm footXfSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), footXfSearchForm);
        map.addAttribute(ENTITYNAME, footXfService.get(id));
        return VIEWPAGE;
    }


}
